#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  securityLock_test
# Version	:  1.0
# Date		:  2023/12/04
# Author	:  yaoxiyao
# Email		:  yaoxiyao@kylinos.com.cn 
# History	:     
#              Version 1.0, 2023/12/04
# Function	:  支持自旋锁、信号量、互斥体等原子操作
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="支持自旋锁、信号量、互斥体等原子操作"

# 目标文件
SecurityLockFile="securityLock_test.c"
SecurityLockOrderFile="securityLock_test"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
        cp ${KERNEL_SRC_DIR_KERNEL}/${SecurityLockFile} ${TmpTestDir_LTFLIB}
        CommRetParse_FailDiy_LTFLIB ${ERROR} "复制文件失败${KERNEL_SRC_DIR_KERNEL}/${SecurityLockFile}"
	return $TPASS
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
        Debug_LLE "rm -rf ${TmpTestDir_LTFLIB}/${SecurityLockFile}"
	rm -rf ${TmpTestDir_LTFLIB}/${SecurityLockFile}
        return $TPASS
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
        testcase_1
        return $TPASS
}


## TODO ： 支持自旋锁、信号量、互斥体等原子操作
#
testcase_1(){
        cd ${TmpTestDir_LTFLIB}
        CommRetParse_LTFLIB "成功进入临时目录${TmpTestDir_LTFLIB}"

        gcc ${SecurityLockFile} -o ${SecurityLockOrderFile} -std=c99 -lpthread
        CommRetParse_LTFLIB "成功编译文件${SecurityLockFile}"

        chmod 777 ${SecurityLockOrderFile}
        CommRetParse_LTFLIB "成功赋权文件${SecurityLockOrderFile}"
          
        # 运行10次程序保证与预期结果一致
        for i in {1…10}
        do
                Info_LLE "运行10次"
                ./${SecurityLockOrderFile}|grep "data not protected by mutex lock"
                if [ "$?" == 0 ];then
                        OutputRet_LTFLIB ${TFAIL}
                        TestRetParse_LTFLIB "测试预期结果不正确"
                fi
        done

        # 运行10次成功才能可以打印以下字符串
        ./${SecurityLockOrderFile}|grep "data error without mutex lock"
        CommRetParse_LTFLIB "不开启安全锁保护,会触发临界区冲突"
        
        ./${SecurityLockOrderFile}|grep "data protected by mutex lock"
        CommRetParse_LTFLIB "开启安全锁保护,不会触发临界区冲突"
}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
